package com.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Attach;
import com.beans.Company;
import com.beans.Msg;
import com.beans.MsgReply;
import com.beans.News;
import com.beans.User;
import com.vo.Page;

public class NewsController extends UniversalController {
	
	public ModelAndView getNews(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map param = new HashMap();
		Map result = new HashMap();
		News news=new News();
		String classid=request.getParameter("classid");
		String id=request.getParameter("id");
		if(classid!=null){
			String title=request.getParameter("title");
			if(title!=null&&!"".equals(title.trim()))
				param.put("title", title);
			param.put("classid", Integer.parseInt(classid));
			Page page = getUniversalManager().findPageByCriteria(this.getPageNo(request), pageSize, param, News.class);
			result.put("o", page);
			return new ModelAndView("news/list", result);
		}else if(classid==null&&id!=null){
			news = (News)getUniversalManager().findByID(News.class, Integer.parseInt(id));
			Map m=new HashMap();
			m.put("pid",news.getId());
			m.put("table", "j_news");
			Page page=getUniversalManager().findPageByCriteria(1, 100, m,Attach.class);
			result.put("o", news);
			result.put("attach", page.getDataList());
			return new ModelAndView("news/news", result);
		}
		return null;
	}
	public ModelAndView form(HttpServletRequest request,
			HttpServletResponse response){
		Map result = new HashMap();
		String classid=request.getParameter("classid");
		String id=request.getParameter("id");
		News news=null;
		if(classid!=null){
			news=new News();
			news.setClassid(Integer.parseInt(classid));
			result.put("o", news);
		}else if(id!=null){
			news=(News)getUniversalManager().findByID(News.class, Integer.parseInt(id));
			result.put("o", news);
		}else{
			news=new News();
			news.setId(0);
			news.setClassid(0);
			result.put("o",news);
		}
		Map m=new HashMap();
		m.put("pid",news.getId());
		m.put("table", "j_news");
		Page page=getUniversalManager().findPageByCriteria(1, 100, m,Attach.class);
		result.put("attach", page.getDataList());
		return new ModelAndView("news/form", result);
	}
	public ModelAndView getSaveNews(HttpServletRequest request,
			HttpServletResponse response){
		String msg;
		News news=new News();
		try {
			String attach=request.getParameter("attach");
			System.out.println(attach);
			String[] attachs=attach.split(",");
			bind(request,news);
			if(news.getId()==0){
				getUniversalManager().insert(news);
				for(String v:attachs){
					if(v!=null&&!"".equals(v)){
						Attach a=new Attach();
						a.setPid(news.getId());
						a.setTable("j_news");
						a.setUrl(v);
						getUniversalManager().insert(a);
					}
				}
			}
			else{
				News news2=(News)getUniversalManager().findByID(News.class,news.getId());
				news2.setContent(news.getContent());
				news2.setTitle(news.getTitle());
				news2.setClassid(news.getClassid());
				getUniversalManager().update(news2);
				//附件删除
				Map m=new HashMap();
				m.put("pid",news.getId());
				Page page=getUniversalManager().findPageByCriteria(1, 100, m, Attach.class);
				getUniversalManager().deleteAll(page.getDataList());
				//附件更新
				for(String v:attachs){
					if(v!=null&&!"".equals(v)){
						Attach a=new Attach();
						a.setPid(news2.getId());
						a.setTable("j_news");
						a.setUrl(v);
						getUniversalManager().insert(a);
					}
				}
			}
			
			msg="操作成功！";
		} catch (Exception e) {
			msg="操作失败！";
			e.printStackTrace();
		}
		getMessage(response, msg);
		return null;
	}
	
	/*
	public ModelAndView addMsg(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map param = new HashMap();
		Map result = new HashMap();
		Msg msg=new Msg();
		User u=(User)request.getSession().getAttribute("user");
		Company c=(Company)request.getSession().getAttribute("company");
		if(u!=null&&c==null) {
			msg.setUserID(u.getId());
			msg.setUserName(u.getRealName());
			result.put("o", u);
			return new ModelAndView("msg/addMsg", result);
		}
		
		if(c!=null){
			msg.setCompanyID(c.getId());
			msg.setCompanyName(c.getcName());
			result.put("o", u);
			return new ModelAndView("msg/addMsg", result);
		}
		return new ModelAndView("login", result);
	}
	public ModelAndView getAddMsgReply(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map result = new HashMap();
		User u=(User)request.getSession().getAttribute("user");
		Company c=(Company)request.getSession().getAttribute("company");
		String hidden=request.getParameter("hidden");
		String hiddenName=request.getParameter("hiddenName");
		String code = (String) request.getParameter("code");
		String validateCode = (String)request.getSession().getAttribute("validateCode");
		if(validateCode==null)
			getMessage(response, "验证码过期！");
		if (null == code||"".equals(code) || !code.toUpperCase().equals(validateCode.toUpperCase())) 
			getMessage(response, "验证码错误！");
		MsgReply m=new MsgReply();
		bind(request,m);
		if(m.getContent()==null||"".equals(m.getContent())){
			getMessage(response, "请输入留言内容！");
			return null;
		}
		if(u!=null){
			m.setUserID(u.getId());
			if("Y".equals(hidden))
				m.setUserName(hiddenName+"(匿名)");
			else 
				m.setUserName(u.getRealName());
			getUniversalManager().insert(m);
			getMessage(response, "留言成功！");
			return null;
		}else if(c!=null){
				m.setCompanyID(c.getId());
				if("Y".equals(hidden))
					m.setCompanyName(hiddenName+"(匿名)");
				else
					m.setCompanyName(c.getcName());
				getUniversalManager().insert(m);
				getMessage(response, "留言成功！");
				return null;
			}
		getMessage(response, "您还没有登录！请登录后发表留言。");
		return null;
	}
	public ModelAndView getShowMsg(HttpServletRequest request,
			HttpServletResponse response){
		Map result = new HashMap();
		String id=request.getParameter("id");
		try{
			Msg msg=(Msg)getUniversalManager().findByID(Msg.class, Integer.parseInt(id));
			result.put("o", msg);
			return new ModelAndView("msg/showMsg",result);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	public ModelAndView insertMsg(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map result = new HashMap();
		Msg msg=new Msg();
		User u=(User)request.getSession().getAttribute("user");
		Company c=(Company)request.getSession().getAttribute("company");
		bind(request,msg);
		msg.setInputdate(new Date());
		Date d=new Date();
		if(d.getMonth()<11)
			d.setMonth(d.getMonth()+1);
		else
			d.setYear(d.getYear()+1);
		if(msg.getEnddate()==null)msg.setEnddate(d);
		result.put("msg", "添加成功！");
		try{
		if(u!=null&&c==null) {
			msg.setUserID(u.getId());
			msg.setUserName(u.getRealName());
			getUniversalManager().insert(msg);
			result.put("o", u);
			return new ModelAndView("msg/addMsg", result);
		}
		
		if(c!=null){
			msg.setCompanyID(c.getId());
			msg.setCompanyName(c.getcName());
			getUniversalManager().insert(msg);
			result.put("o", u);
			return new ModelAndView("msg/addMsg", result);
		}
		}catch(Exception e){
			result.put("msg", "添加失败！");
			return new ModelAndView("msg/addMsg", result);
		}
		return null;
	}
	public ModelAndView msgReplyList(HttpServletRequest request,
			HttpServletResponse response){
		Map result = new HashMap();
		Map param=new HashMap();
		String pid=request.getParameter("pid");
		String tableName=request.getParameter("tableName");
		try{
			param.put("tableName", tableName);
			param.put("pid",Integer.parseInt(pid));
			Page page=getUniversalManager().findPageByCriteria(this.getPageNo(request), pageSize, param, MsgReply.class);
			result.put("o", page);
			return new ModelAndView("msg/msgReplyList",result);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}*/
}
